fix(entities): 修复 InterAction.getEnvType 对 chat_type 的空指针 - #1
Open
AnnaofArendelle wants to merge 1 commit into
Open
fix(entities): 修复 InterAction.getEnvType 对 chat_type 的空指针#1AnnaofArendelle wants to merge 1 commit into
AnnaofArendelle wants to merge 1 commit into
Conversation
chat_type 声明为 Integer,getEnvType() 中的 `chat_type == 0` 会触发拆箱。
QQ 下发的 InterAction 事件并非总是携带该字段,此时抛出 NullPointerException。
崩溃点在 Events.onEvent 的日志格式化路径:
logger.info(String.format("Bot(%s) post(%s) from %s", ..., event, ...));
-> BaseInterActionEvent.toString() -> InterAction.getCid() -> getEnvType()
java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()"
because "this.chat_type" is null
at io.github.kloping.qqbot.entities.qqpd.InterAction.getEnvType(InterAction.java:75)
at io.github.kloping.qqbot.entities.qqpd.InterAction.getCid(InterAction.java:65)
at io.github.kloping.qqbot.impl.BaseInterActionEvent.toString(BaseInterActionEvent.java:69)
at io.github.kloping.qqbot.network.Events.onEvent(Events.java:115)
事件本身已分发完毕,功能不受影响,但每次都会向 stderr 打印一次堆栈。
改为先判空;chat_type 缺失时回退到 scene 字段——按官方文档,二者承载同一信息
(guild / group / c2c)。两者皆缺失时按群聊处理,与 chat_type 非 0 时的既有行为一致。
chat_type 非空时行为完全不变。
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
InterAction.chat_type声明为Integer,而getEnvType()中写的是chat_type == 0,会触发自动拆箱。QQ 下发的 InterAction 事件并非总是携带该字段,此时抛出 NPE。崩溃发生在
Events.onEvent的日志格式化路径上:事件本身在这一行之前就已分发完毕,功能不受影响,但每收到一次这类事件就会向 stderr 打印一次完整堆栈。
复现
在真实环境中稳定出现:机器人接入 QQ 群后,群内 @ 机器人或触发交互组件时,日志中周期性出现上述堆栈。本次是在一台 Minecraft 服务端上跑机器人时发现的,一个会话内出现 4 次。
改动
getEnvType()先判空;chat_type缺失时回退到scene字段 —— 按类上 Javadoc 引用的官方文档,二者承载同一信息(guild/group/c2c)。两者皆缺失时按群聊处理,与chat_type非 0 时的既有行为保持一致。chat_type非空时行为完全不变,不影响任何现有调用方。验证
已在引入本仓库源码的项目中编译通过(
compileJavaBUILD SUCCESSFUL)。备注
刻意保持最小改动,没有一并处理
chat_type == 2(单聊)目前被归入GROUP分支、以及getCid()在单聊场景返回group_openid的问题 —— 那涉及语义判断,留给维护者定夺。